package net;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.passClass.PassData;
import util.MD5;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class ToServer {
    private static String serverHost;
    private static final int serverPort = 10086;
    static {
        try {
            serverHost = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static PassData receive(String path, String send){
        System.out.println("args:"+send);

        ObjectMapper om = new ObjectMapper();
        PassData data = new PassData(false,null,"操作失败");

        try{
            Socket server = new Socket(serverHost,serverPort);
            OutputStream out = server.getOutputStream();
            InputStream in = server.getInputStream();

            out.write(path.getBytes());         //传入路径
            out.flush();

            byte[] bytes = new byte[4096];
            int len = in.read(bytes);           //等待服务器响应
            String tmp = new String(bytes,0,len);
            System.out.println("Server:"+tmp);
            if(!tmp.equals("received")){
                return data;
            }

            out.write(send.getBytes());         //传入参数
            out.flush();

            len = in.read(bytes);               //等待接收响应数据
            String type = new String(bytes,0,len);
            data = (PassData) om.readValue(in,Class.forName(type));
            System.out.println("messages:"+data.getMessage());
            System.out.println("data:"+data.getData());

            in.close();
            out.close();
            server.close();

        } catch (SocketException e) {
            System.out.println("Server off-line.");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return data;
    }

    public static void main(String[] args) {

        /*
         * e.g. of how to communicate with server
         */

        System.out.println(MD5.md5Encryption("123"));//-> ICy5YqxZB1uWSwcVLSNLcA==
        System.out.println(MD5.md5Encryption("321"));//-> yvGj37UF/+0NAkEw9Yxc+g==

        //记忆清空
        try {
            File fID = new File("UID_Number.txt");
            FileWriter out = new FileWriter(fID);
            out.write("UID=" + 2020);
            out.close();

            File fDB = new File("Building_DB.json");
            out = new FileWriter(fDB);
            out.write("null");
            out.close();

            out = new FileWriter("Patient_DB.json");
            out.write("null");
            out.close();

            out = new FileWriter("Question_DB.json");
            out.write("null");
            out.close();

            out = new FileWriter("Staff_DB.json");
            out.write("null");
            out.close();

            out = new FileWriter("Template_DB.json");
            out.write("null");
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(ToServer.receive("/user/staff/add", "username:123\npassword:123").getMessage());
        System.out.println(ToServer.receive("/user/staff/add", "username:123\npassword:123").getMessage());
        System.out.println(ToServer.receive("/user/login", "username:123\npassword:123").getMessage());
        System.out.println(ToServer.receive("/user/login", "username:123\npassword:ICy5YqxZB1uWSwcVLSNLcA==").getMessage());
        System.out.println(ToServer.receive("/user/staff/update", "uid:S1854877\nusername:123\npassword:321").getMessage());
        System.out.println(ToServer.receive("/user/login", "username:123\npassword:ICy5YqxZB1uWSwcVLSNLcA==").getMessage());
        System.out.println(ToServer.receive("/user/login", "username:123\npassword:yvGj37UF/+0NAkEw9Yxc+g==").getMessage());
        System.out.println(ToServer.receive("/user/staff/add", "username:321\npassword:321").getMessage());
        System.out.println(ToServer.receive("/user/staff/delete", "uid:S1854877").getMessage());
        System.out.println(ToServer.receive("/user/login", "username:123\npassword:yvGj37UF/+0NAkEw9Yxc+g==").getMessage());
        System.out.println(ToServer.receive("/user/login", "username:321\npassword:yvGj37UF/+0NAkEw9Yxc+g==").getMessage());
        System.out.println(ToServer.receive("/user/staff/add", "username:000\npassword:321").getMessage());
        System.out.println(ToServer.receive("/user/staff/update", "username:000\npassword:321\nuid:S4757701\nname:姓名0\nbirthday:10000000\nspecialty:专长0\ntel:123\nidNumber:1234\ntitle:0").getMessage());
        System.out.println(ToServer.receive("/user/staff/add", "username:001\npassword:321").getMessage());
        System.out.println(ToServer.receive("/user/staff/update", "username:001\npassword:321\nuid:S6209113\nname:姓名0\nbirthday:10000000\nspecialty:专长1\ntel:123\nidNumber:123\ntitle:0").getMessage());
        System.out.println(ToServer.receive("/user/staff/add", "username:002\npassword:321").getMessage());
        System.out.println(ToServer.receive("/user/staff/update", "username:002\npassword:321\nuid:S7660525\nname:姓名0\nbirthday:10000000\nspecialty:专长2\ntel:123\nidNumber:12\ntitle:1").getMessage());
        System.out.println(ToServer.receive("/user/staff/add", "username:003\npassword:321").getMessage());
        System.out.println(ToServer.receive("/user/staff/update", "username:003\npassword:321\nuid:S9111937\nname:姓名0\nbirthday:10000000\nspecialty:专长3\ntel:123\nidNumber:123\ntitle:2").getMessage());
        System.out.println(ToServer.receive("/user/staff/get", "idNumber:123").getData());


        System.out.println(ToServer.receive("/patient/add", "name:1\nsex:1\nbirthday:0\nidNumber:2\ntel:1\nemergencyContact:1\nemergencyTel:2\ndoctorUID:1").getMessage());
        System.out.println(ToServer.receive("/patient/add", "name:2\nsex:1\nbirthday:0\nidNumber:2\ntel:1\nemergencyContact:1\nemergencyTel:2\ndoctorUID:1").getMessage());
        System.out.println(ToServer.receive("/patient/add", "name:3\nsex:1\nbirthday:0\nidNumber:2\ntel:1\nemergencyContact:1\nemergencyTel:2\ndoctorUID:1").getMessage());
        System.out.println(ToServer.receive("/patient/update", "uid:P0563358\nname:10000\nsex:10000\nbirthday:100000000\nidNumber:20000\ntel:10000\nemergencyContact:1000\nemergencyTel:2000\ndoctorUID:D100000").getMessage());
        System.out.println(ToServer.receive("/patient/delete", "uid:P0563358").getMessage());
        System.out.println(ToServer.receive("/patient/get", "sex:1").getData());

        System.out.println(ToServer.receive("/hospital/room/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:2").getMessage());
        System.out.println(ToServer.receive("/hospital/room/add", "buildingNumber:1\nfloorNumber:2\nroomNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/add", "buildingNumber:2\nfloorNumber:1\nroomNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/delete", "buildingNumber:1\nfloorNumber:1\nroomNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/get", "buildingNumber:1").getData());
        System.out.println(ToServer.receive("/hospital/bed/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:1\nbedNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/bed/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:1\nbedNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/bed/add", "buildingNumber:1\nfloorNumber:1\nroomNumber:1\nbedNumber:2").getMessage());
        System.out.println(ToServer.receive("/hospital/room/get", "buildingNumber:1").getData());
        System.out.println(ToServer.receive("/hospital/bed/check_in", "buildingNumber:1\nfloorNumber:1\nroomNumber:1\nbedNumber:2\npatientUID:P3466182").getMessage());
        System.out.println(ToServer.receive("/hospital/room/get", "buildingNumber:1").getData());
        System.out.println(ToServer.receive("/patient/get", "uid:P2014770").getData());
        System.out.println(ToServer.receive("/hospital/bed/exchange", "buildingNumber:1\nfloorNumber:1\nroomNumber:1\nbedNumber:1\nbuildingNumber2:1\nfloorNumber2:1\nroomNumber2:1\nbedNumber2:2").getMessage());
        System.out.println(ToServer.receive("/hospital/room/get", "buildingNumber:1").getData());
        System.out.println(ToServer.receive("/patient/get", "uid:P2014770").getData());
        System.out.println(ToServer.receive("/hospital/bed/check_out", "buildingNumber:1\nfloorNumber:1\nroomNumber:1\nbedNumber:1").getMessage());
        System.out.println(ToServer.receive("/hospital/room/get", "buildingNumber:1").getData());
        System.out.println(ToServer.receive("/patient/get", "uid:P2014770").getData());

        System.out.println(ToServer.receive("/assessment/question/add", "name:1\ntype:1\nanswer_0:0\nanswer_1:1\nanswer_2:2").getMessage());
        System.out.println(ToServer.receive("/assessment/question/add", "name:1\ntype:1\nanswer_0:1\nanswer_1:1\nanswer_2:1").getMessage());
        System.out.println(ToServer.receive("/assessment/question/add", "name:2\ntype:1\nanswer_0:000\nanswer_1:111\nanswer_2:222").getMessage());
        System.out.println(ToServer.receive("/assessment/question/update", "uid:Q0563358\nname:1\ntype:1\nanswer_0:1\nanswer_1:1\nanswer_2:1").getMessage());
        System.out.println(ToServer.receive("/assessment/question/delete", "uid:Q0563358").getMessage());
        System.out.println(ToServer.receive("/assessment/question/add", "name:1\ntype:1\nanswer_0:1\nanswer_1:1\nanswer_2:1").getMessage());
        System.out.println(ToServer.receive("/assessment/question/get", "type:1").getData());

        System.out.println(ToServer.receive("/assessment/template/add", "name:1\ntype:1").getMessage());
        System.out.println(ToServer.receive("/assessment/template/add", "name:1\ntype:1").getMessage());
        System.out.println(ToServer.receive("/assessment/template/add", "name:2\ntype:1").getMessage());
        System.out.println(ToServer.receive("/assessment/template/update", "uid:T0723251\nname:1\ntype:1\nanswer_0:1\nanswer_1:1\nanswer_2:1").getMessage());
        System.out.println(ToServer.receive("/assessment/template/delete", "uid:T0723251").getMessage());
        System.out.println(ToServer.receive("/assessment/template/add", "name:1\ntype:1").getMessage());
        System.out.println(ToServer.receive("/assessment/template/get", "type:1").getData());

        System.out.println(ToServer.receive("/patient/test", "uid:P2014770\ntemplateUID:T3626075\nmark:100\ntime:123456789").getMessage());
        System.out.println(ToServer.receive("/patient/get", "uid:P2014770").getData());

    }
}
